﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace EasyStudy.Logic.Time
{
    public class TimeMgr
    {
        public static List<long> GetTaskCircle() 
        {
            List<long> result = new List<long>();

            // 读取配置文件
            EasyStudy.Properties.Settings setting = new Properties.Settings();
            string strCircle = setting.RemindCircle;
            string[] allCircle = strCircle.Split(";".ToCharArray(), 
                StringSplitOptions.RemoveEmptyEntries);

            foreach (string timeWithUnit in allCircle)
            {
                result.Add(ToTimeStamp(timeWithUnit));
            }

            return result;
        }

        /// <summary>
        /// 获取一周的时间
        /// </summary>
        /// <returns>返回DateTime[7]</returns>
        public static DateTime[] GetPreviousAWeekTime()
        {
            DateTime[] result = new DateTime[7];

            DateTime now = DateTime.Now;

            for (int i = 1; i < 8; ++i)
            {
                result[i - 1] = now.AddDays(i * (-1));
            }

            return result;
        }

        public static long ToTimeStamp(String timeWithUnit) 
        {
            long result = 0;

            Regex reg = new Regex(@"(\d+)(\w+)", 
                RegexOptions.Multiline | RegexOptions.Singleline);

            if (!reg.IsMatch(timeWithUnit))
            {
                result = 0;
            }
            else 
            {
                MatchCollection matches = reg.Matches(timeWithUnit);
                if (matches != null && matches.Count > 0)
                {
                    string num = matches[0].Groups[1].Value.ToLower();
                    string unit = matches[0].Groups[2].Value.ToLower();
                    long nNum = 0;
                    int nUnit = 0;

                    switch (unit) 
                    {
                        case "mins":
                            nUnit = 60;
                            break;
                        case "hours":
                            nUnit = 60 * 60;
                            break;
                        case "days":
                            nUnit = 24 * 3600;
                            break;
                        case "months":
                            nUnit = 30 * 24 * 3600;
                            break;
                        case "years":
                            nUnit = 12 * 30 * 24 * 3600;
                            break;
                        default:
                            nUnit = 0;
                            break;
                    }

                    if (long.TryParse(num, out nNum))
                    {
                        result = nNum * nUnit;
                    }
                    else
                    {
                        result = 0;
                    }
                }
            }

            return result;
        }
    }
}
